<!-- 
 * @Description:
 * @Date: 2024-06-25 21:24:10
 * @LastEditTime: 2024-09-13 10:53:35
-->
<!-- 使用 type="home" 属性设置首页，其他页面不需要设置，默认为page；推荐使用json5，更强大，且允许注释 -->
<route lang="json5" type="page">
{
  style: {
    navigationBarTitleText: '详情',
  },
}
</route>
<template>
  <view class="bg-#fff overflow-auto w-100% h-screen box-border p-20rpx">
    <view class="flex items-center">
      <view><wd-button plain size="small">已完成</wd-button></view>
      <view class="completed-tip">
        {{ detailList.isCorrect ? '已通过：' : '未通过：' }}共{{ recordList.total }}题|答对{{
          recordList.correct
        }}道|答错{{ recordList.wrong }}道
      </view>
    </view>
    <view class="tobe-title mt-30rpx">{{ detailList.title }}</view>
    <view class="tobe-time">发布日期： {{ formatTime(detailList.createTime, 'yyyy-MM-dd') }}</view>
    <view class="tobe-line"></view>
    <view v-html="detailList.content"></view>
  </view>
</template>

<script lang="ts" setup>
import { getTobeDetail } from '@/service/partBuilding/index'
import { formatTime, updateImageUrls } from '@/utils/index'
const detailList = ref({})
const recordList = ref({})
const optionList = ref({})
const saveId = ref()
const params = reactive({
  id: optionList.value,
  userId: saveId.value,
})
const getDetails = async () => {
  const data: any = await getTobeDetail({ ...params })
  detailList.value = data.data
  detailList.value.content = updateImageUrls(detailList.value.content)

  const parts = data.data.result.split('-')
  const total = parseInt(parts[0])
  const correct = parseInt(parts[1])
  const wrong = parseInt(parts[2])
  recordList.value.correct = correct || 0
  recordList.value.wrong = wrong || 0
  recordList.value.total = total || 0
}

// 获取资讯详情
onLoad(async (options) => {
  optionList.value = options.id
  uni.getStorageInfo({
    success: function () {
      // 单独获取 'user' 键的值
      uni.getStorage({
        key: 'user',
        success: function (res) {
          const data = JSON.parse(res.data)
          saveId.value = data.userInfo.residentId
          params.userId = saveId.value
          params.id = options.id
          getDetails()
        },
        fail: function (err) {
          console.error('获取 user 失败:', err)
        },
      })
    },
  })
})
</script>
<style lang="scss">
::v-deep .wd-button.is-plain.is-primary {
  font-size: 28rpx !important;
  color: rgba(188, 13, 13, 1) !important;
  border-color: rgba(188, 13, 13, 1) !important;
}
.tobe-line {
  margin: 24rpx 0;
  border-bottom: 2rpx solid #ccc;
}
.tobe-title {
  margin-bottom: 28rpx;
  font-size: 36rpx;
  line-height: 50rpx;
  color: rgba(27, 27, 27, 1);
  text-align: justify;
}
.tobe-time {
  font-size: 28rpx;
  line-height: 40rpx;
  color: rgba(113, 113, 113, 1);
  text-align: justify;
}

.completed-tip {
  margin-left: 12rpx;
  font-size: 28rpx;
  line-height: 66rpx;
  color: rgba(188, 13, 13, 1);
  text-align: left;
}
rich-text {
  line-height: 1.7;
}
</style>
